package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.DefGainReview;
import com.ruoyi.system.domain.request.CompanyRequest;
import com.ruoyi.system.domain.request.GainRequest;
import com.ruoyi.system.domain.vo.DefGainListVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DefGain;
import com.ruoyi.system.service.IDefGainService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 成果管理Controller
 * 
 * @author ruoyi
 * @date 2024-10-11
 */
@RestController
@RequestMapping("/system/gain")
public class DefGainController extends BaseController
{
    @Autowired
    private IDefGainService defGainService;

    /**
     * 查询成果管理列表
     */
    @GetMapping("/list")
    public TableDataInfo pages(GainRequest request)
    {
        startPage();
        if (request.getDrillId() == null) {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            request.setDrillId(loginUser.getDrillId());
        }
        List<DefGainListVo> list = defGainService.pageList(request);
        return getDataTable(list);
    }

    /**
     * 导出成果管理列表
     */
    @Log(title = "成果管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DefGain defGain)
    {
        List<DefGain> list = defGainService.selectDefGainList(defGain);
        ExcelUtil<DefGain> util = new ExcelUtil<DefGain>(DefGain.class);
        util.exportExcel(response, list, "成果管理数据");
    }

    /**
     * 获取成果管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult detail(@PathVariable("id") Long id)
    {
        return success(defGainService.detail(id));
    }

    /**
     * 新增成果管理
     */
    @Log(title = "成果管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody DefGain defGain)
    {
        return toAjax(defGainService.submitGain(defGain));
    }

    /**
     * 修改成果管理
     */
    @PreAuthorize("@ss.hasPermi('system:gain:edit')")
    @Log(title = "成果管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DefGain defGain)
    {
        return toAjax(defGainService.updateDefGain(defGain));
    }

    /**
     * 删除成果管理
     */
    @PreAuthorize("@ss.hasPermi('system:gain:remove')")
    @Log(title = "成果管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(defGainService.deleteDefGainByIds(ids));
    }

    /**
     * 评审成果
     */
    @PostMapping("gainReview")
    @Log(title = "成果管理", businessType = BusinessType.UPDATE)
    public AjaxResult gainReview(@Valid @RequestBody DefGainReview gainReview)
    {
    	return toAjax(defGainService.gainReview(gainReview));
    }

    /**
     * 成果申诉
     */
    @PostMapping("gainAppeal")
    @Log(title = "成果管理", businessType = BusinessType.UPDATE)
    public AjaxResult gainAppeal(@RequestBody DefGainReview gainReview)
    {
        return toAjax(defGainService.gainAppeal(gainReview));
    }

    /**
     * 成果申诉评审
     */
    @PostMapping("appealReview")
    @Log(title = "成果管理", businessType = BusinessType.UPDATE)
    public AjaxResult appealReview(@Valid @RequestBody DefGainReview gainReview)
    {
        return toAjax(defGainService.gainAppealReview(gainReview));
    }

    /**
     * 成果统计
     */
    @GetMapping("count")
    public AjaxResult count(GainRequest request)
    {
    	return success(defGainService.gainCount(request));
    }

    /**
     * 团队成果统计
     * orderBy 0 倒序 1 正序
     */
    @GetMapping("teamCount")
    public AjaxResult teamCount(Integer orderBy)
    {
    	return success(defGainService.teamCount(orderBy));
    }

    /**
     * 攻防单位
     */
    @GetMapping("companys")
    public TableDataInfo company(CompanyRequest request) {
    	return defGainService.companyPage(request);
    }

    /**
     * 演练详情 攻击团队、防守团队统计
     */
    @GetMapping("teamTypeCount")
    public AjaxResult teamTypeCount() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return success(defGainService.teamTypeCount(loginUser.getDrillId()));
    }

    /**
     * 队伍得分，排名
     * */
    @GetMapping("teamScore")
    public AjaxResult teamScore(Integer teamId) {

    	return success(defGainService.getTeamScore());
    }
}
